package com.duk.main;

import java.net.URL;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.duk.util.SearchAdapter;
import com.rss_reader.data.RSSFeed;
import com.rss_reader.data.RSSItem;
import com.rss_reader.data.StaticURL;
import com.rss_reader.sax.RSSHandler;

public class NewestActivity extends ActivityGroup implements OnItemClickListener {
	private ListView itemlist;
	public SearchAdapter adapter;
	private RSSFeed feed = null;
	public final String RSS_URL = StaticURL.RSS_NEWEST_URL;
	 @Override  
	    public void onCreate(Bundle savedInstanceStated) {   
		  	super.onCreate(savedInstanceStated);   
	        setContentView(R.layout.newest_view);   
	        itemlist = (ListView) findViewById(R.id.news_listview);   
	        feed =  getFeed(RSS_URL);
	        showListView(feed);
	    }   
	 private RSSFeed getFeed(String urlString)
	    {
	    	try
	    	{
	    	   URL url = new URL(urlString);

	           SAXParserFactory factory = SAXParserFactory.newInstance();
	           SAXParser parser = factory.newSAXParser();
	           XMLReader xmlreader = parser.getXMLReader();           
	           RSSHandler rssHandler = new RSSHandler();
	           xmlreader.setContentHandler(rssHandler);
	           InputSource is = new InputSource(url.openStream());             
	           xmlreader.parse(is);
	  
	           return rssHandler.getFeed();
	    	}
	    	catch (Exception ee)
	    	{
	    			ee.printStackTrace();
	    		return null;
	    	}
	    }

	    
	    private void showListView(RSSFeed feedSoruce)
	    {
	        
	        if (feedSoruce == null)
	        {
	        	setTitle("报纸订阅器");
	        	return;
	        }
	        
	        adapter = new SearchAdapter(this,
	        		feedSoruce.getAllItemsForListView(), R.layout.listitem, 
					new String[] { RSSItem.TITLE, RSSItem.DESCRIPTION, RSSItem.IMAGE_PATH }, new int[] {
							R.id.listitem_title, R.id.listitem_content,
							R.id.listitem_pic });
	        itemlist.setAdapter(adapter);
	        itemlist.setOnItemClickListener(this);  
	        itemlist.setSelection(0);
	        
	    }
	    public void onItemClick(AdapterView parent, View v, int position, long id)
	     {
	    	 Intent itemintent = new Intent(this,ActivityShowDescription.class);
	         
	    	 Bundle b = new Bundle();
	    	 b.putString("title", feed.getItem(position).getTitle());
	    	 b.putString("description", feed.getItem(position).getDescription());
	    	 b.putString("link", feed.getItem(position).getLink());
	    	 b.putString("pubdate", feed.getItem(position).getPubDate());
	    	 b.putString("content", feed.getItem(position).getContent());
	    	 b.putSerializable("image_path", feed.getItem(position).getImage_path());
	    	 itemintent.putExtra("android.intent.extra.rssItem", b);
	         startActivityForResult(itemintent, 0);
	     }
	    private Dialog ExitDialog(Context context) {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setIcon(R.drawable.icon);
			builder.setTitle("生活新报提示");
			builder.setMessage("确定退出么?");
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
							finish();
						}
					});
			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {

						}
					});
			return builder.create();
		}
	   
	    @Override
		public boolean onKeyDown(int keyCode, KeyEvent event) {
			if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
				ExitDialog(NewestActivity.this).show();
				return true;
			}
			return super.onKeyDown(keyCode, event);
		}
}
